﻿using UnityEngine;
using Entitas;
using UnityEngine.Tilemaps;
class PhysicsImplementation : IPhysicsService
{
    public bool CastRayToCollider<T>(T entity ,EntitasVec3 origin, Direction direction) where T : class,IEntity
    {
       
        bool hitcollider = false;
        var Unity_origin = (Vector3)origin;
        var Unity_Direction = new Vector3();
        Unity_origin.y -= .5f;

        switch (direction)
        {
            case Direction.Up:
                Unity_Direction = Vector3.up;
                break;
            case Direction.Down:
                Unity_Direction = Vector3.down;
                break;
            case Direction.Left:
                Unity_Direction = Vector3.left;
                break;
            case Direction.Right:
                Unity_Direction = Vector3.right;
                break;
            case Direction.Up_Right:
                Unity_Direction = new Vector3(1, 1);
                break;
            case Direction.Up_Left:
                Unity_Direction = new Vector3(-1, 1);
                break;
            case Direction.Down_Right:
                Unity_Direction = new Vector3(1, -1);
                break;
            case Direction.Down_Left:
                Unity_Direction = new Vector3(-1, -1);
                break;
            default:
                break;
        }
        var rayhits=Physics2D.RaycastAll(Unity_origin, Unity_Direction,1f);
       
        if(entity is PlayerEntity)
        {
            foreach(var e in rayhits)
            {
                if(e.collider is TilemapCollider2D || e.collider.GetComponent<EnemyView>() != null)
                {
                    Debug.DrawRay(Unity_origin, Unity_Direction, Color.red, 1f);
                    hitcollider = true;
                }
                else Debug.DrawRay(Unity_origin, Unity_Direction, Color.blue, 1f);
            }
        }

        else
        {
            var NameEntity = entity as INameEntity;
            foreach(var e in rayhits)
            {
                if(e.collider is TilemapCollider2D || e.collider.GetComponent<PlayerView>() != null||e.collider.name!=NameEntity.name.value)
                {
                    Debug.DrawRay(Unity_origin, Unity_Direction, Color.red, 1f);
                    hitcollider = true;
                }
                else Debug.DrawRay(Unity_origin, Unity_Direction, Color.blue, 1f);
            }
        }
        return hitcollider;      
    }
}

